package com.xique.door.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.HouseClaim;
import com.xique.door.bean.request.buildHouse.HouseClaimPageRequest;
import com.xique.door.bean.search.build.HouseClaimListSearch;
import com.xique.door.mapper.HouseClaimMapper;
import com.xique.door.service.IHouseClaimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2022/4/19 9:52
 */
@Service
public class HouseClaimServiceImpl extends ServiceImpl<HouseClaimMapper, HouseClaim> implements IHouseClaimService {

    @Autowired
    private HouseClaimMapper houseClaimMapper;

    @Override
    public IPage<HouseClaim> selectHouseClaimPage(HouseClaimPageRequest request) {

        QueryWrapper<HouseClaim> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getParkId())) {
            condition.eq("park_id", request.getParkId());
        }
        if (StringUtils.isNotNull(request.getAreaId())) {
            condition.eq("area_id", request.getAreaId());
        }
        if (StringUtils.isNotNull(request.getBuildingId())) {
            condition.eq("building_id", request.getBuildingId());
        }
        if (StringUtils.isNotNull(request.getHouseId())) {
            condition.eq("house_id", request.getHouseId());
        }
        if (StringUtils.isNotEmpty(request.getUserName())) {
            condition.like("user_name", request.getUserName());
        }
        if (StringUtils.isNotEmpty(request.getPhone())) {
            condition.like("phone", request.getPhone());
        }
        if (StringUtils.isNotNull(request.getIdentityType())) {
            condition.eq("identity_type", request.getIdentityType());
        }
        if (StringUtils.isNotNull(request.getStatus())) {
            condition.eq("status", request.getStatus());
        }
        condition.orderByDesc("apply_time");
        Page<HouseClaim> page = new Page<>();
        if (StringUtils.isNotNull(request.getPageNum()) && StringUtils.isNotNull(request.getPageSize())) {
            page.setCurrent(request.getPageNum());
            page.setSize(request.getPageSize());
        }

        return houseClaimMapper.selectPage(page, condition);
    }

    @Override
    public List<HouseClaim> selectHouseClaimList(HouseClaimListSearch search) {
        QueryWrapper<HouseClaim> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        if (StringUtils.isNotNull(search.getBaseUserId())) {
            condition.eq("base_user_id", search.getBaseUserId());
        }
        if (StringUtils.isNotNull(search.getHouseId())) {
            condition.eq("house_id", search.getHouseId());
        }
        if (CollectionUtil.isNotEmpty(search.getStatuses())) {
            condition.in("status", search.getStatuses());
        }
        condition.orderByDesc("apply_time");
        return houseClaimMapper.selectList(condition);
    }
}
